package com.nf.xiaomi.dao.impl;

import com.nf.dbutils.SqlExecutor;
import com.nf.dbutils.handlers.BeanListHandler;
import com.nf.xiaomi.dao.ProductDetailDao;
import com.nf.xiaomi.entity.ProductEntity;
import com.nf.xiaomi.util.SqlExecutorUtils;

import java.util.List;

public class ProductDetailDaoImpl implements ProductDetailDao {
    SqlExecutor executor = SqlExecutorUtils.getExecutor();

    public List<ProductEntity> typeAll(Integer categoryID) {
        String sql = "select pd.product_detail_id,p.product_id,p.category_id,p.product_name,a.attribute_list,pd.product_detail_price,pd.product_detail_amount,pd.product_picture_list,p.product_price,p.product_picture,p.product_describe,p.product_date,p.nstate from product_detail pd " +
                "inner join product p on pd.product_id = p.product_id " +
                "inner join attribute a on a.attribute_id = pd.attribute_id\n" +
                "where p.category_id = ?";
        return executor.query(sql, new BeanListHandler<>(ProductEntity.class), categoryID);
    }

    @Override
    public List<ProductEntity> getAll() {
        String sql = "select pd.product_detail_id,p.product_id,p.category_id,p.product_name,a.attribute_list,pd.product_detail_price,pd.product_detail_amount,pd.product_picture_list,p.product_price,p.product_picture,p.product_describe,p.product_date,p.nstate from product_detail pd " +
                "inner join product p on pd.product_id = p.product_id " +
                "inner join attribute a on a.attribute_id = pd.attribute_id";
        return executor.query(sql, new BeanListHandler<>(ProductEntity.class));
    }

    @Override
    public List<ProductEntity> getAll(Integer productID) {
        String sql = "select pd.product_detail_id,p.product_id,p.category_id,p.product_name,a.attribute_list,pd.product_detail_price,pd.product_detail_amount,pd.product_picture_list,p.product_price,p.product_picture,p.product_describe,p.product_date,p.nstate from product_detail pd " +
                "inner join product p on pd.product_id = p.product_id " +
                "inner join attribute a on a.attribute_id = pd.attribute_id\n" +
                "where pd.product_detail_id = ?";
        BeanListHandler<ProductEntity> listHandler = new BeanListHandler<>(ProductEntity.class);
        return executor.query(sql, listHandler, productID);
    }

    public List<ProductEntity> getAll(String name) {
        String sql = "select pd.product_detail_id,p.product_id,p.category_id,p.product_name,a.attribute_list,pd.product_detail_price,pd.product_detail_amount,pd.product_picture_list,p.product_price,p.product_picture,p.product_describe,p.product_date,p.nstate from product_detail pd " +
                "inner join product p on pd.product_id = p.product_id " +
                "inner join attribute a on a.attribute_id = pd.attribute_id\n" +
                "where p.product_name like ?";
        BeanListHandler<ProductEntity> listHandler = new BeanListHandler<>(ProductEntity.class);
        return executor.query(sql, listHandler, "%"+name+"%");
    }

    @Override
    public Integer insert(ProductEntity entity) {
        String sql = "insert into product_detail(product_id,attribute_id,product_name,product_picture_list,product_detail_price,product_detail_amount,nstate) " +
                "values(?,?,?,?,?,?,?)";
        return executor.update(sql,entity.getProductID(),entity.getAttributeID(),entity.getProductName(),entity.getProductDetailPrice(),entity.getProductDetailPrice(),entity.getProductDetailAmount(),entity.getState());
    }

    @Override
    public Integer update(ProductEntity entity) {
        String sql="update product_detail set product_id=?,attribute_id=(select attribute_id from attribute where attribute_list=?),product_name=?,product_picture_list=?,product_detail_price=?,product_detail_amount=?,nstate=? where product_detail_id = ?";
        System.out.println("entity.getAttributeList() = " + entity.getAttributeList());
        return executor.update(sql,entity.getProductID(),entity.getAttributeList(),entity.getProductName(),entity.getProductDetailPrice(),entity.getProductDetailPrice(),entity.getProductDetailAmount(),entity.getState(),entity.getProductDetailID());
    }

    @Override
    public Integer deleteById(Integer id) {
        String sql = "delete from product_detail where product_detail_id = ?";
        return executor.update(sql, id);
    }

    //这个暂时别动
    @Override
    public List<ProductEntity> listByCid(int cid) {
        String sql="select id,name,price from product where category_id =?";
        BeanListHandler<ProductEntity> handler=new BeanListHandler<>(ProductEntity.class);
        return executor.query(sql, handler,cid);
    }
}

